package com.sky.controller.admin;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author 符健清
 * @version 1.0
 *用于文件上传
 *
 */
@SuppressWarnings({"all"})
@Slf4j
@RestController
public class Updowload {
    @Autowired
   private AliOssUtil aliOssUtil;
    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file) throws Exception {
        //        日志输出
        log.info("图像路径:{} " ,file.getOriginalFilename());
//       调用阿里云工具类
        byte[] bytes = file.getBytes();
//       获取文件的全名
        String originalFilename = file.getOriginalFilename();
//        截取后缀.jpg
//        使用uuid 工具拼接字符
        originalFilename = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName= UUID.randomUUID().toString()+originalFilename;
        String upload = aliOssUtil.upload(bytes, fileName);
        Result<Object> result = new Result<>();
         return result.success(upload);
    }
}
